//
//  NMNwrBlock.m
//  podrow
//
//  Created by Michael Xu on 4/27/14.
//  Copyright (c) 2014 podrow. All rights reserved.
//

#import "NMPodrow.h"
#import "NMDeviceInfo.h"
#import "NMNwrBlock.h"
#import "AFNetworking.h"
#import "NSMutableDictionary+UrlGenerate.h"

@implementation NMNetwinModel
@end

@implementation NMNwrBlock

+ (void)startRequestWithBlock:(ServerResponseBlock)serverResponseBlock
{
    NSURL *szURL = [NSURL URLWithString:[NSString stringWithFormat:@"%@/Service/Sponsors", [UserInfo sharedInstance].NWR_HOST]];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:szURL];
    [request setHTTPMethod:@"POST"];
    [request setValue:@"application/json; encoding=utf-8" forHTTPHeaderField:@"Content-Type"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    NSString *ua = [NSString stringWithFormat:@"%@ %@ (%@; %@; %@)", [NMDeviceInfo getBundleId], [NMDeviceInfo getAppVersion], [NMDeviceInfo getOSVersion], [NMDeviceInfo getMachineDetail], [NMDeviceInfo getLanguage]];
    [request setValue:ua forHTTPHeaderField:@"User-Agent"];
    [request setValue:@"gzip" forHTTPHeaderField:@"Accept-Encoding"];
    
    //
    NSDictionary *dict = @{@"mid":@"0000000000000000000"};
    NSData *requestData =  [NSJSONSerialization dataWithJSONObject:dict options:0 error:nil];
    [request setValue:[NSString stringWithFormat:@"%lu", (unsigned long)requestData.length] forHTTPHeaderField:@"Content-Length"];
    [request setHTTPBody:requestData];
    
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSError *error;
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:operation.responseData
                                                             options:NSJSONReadingMutableLeaves
                                                               error:&error];
        NSArray *array = [dict objectForKey:@"sponsors"];
        if (error) {
            serverResponseBlock(nil, error);
        }
        else {
            NSMutableArray *ret = [[NSMutableArray alloc] init];
            for (NSDictionary *data in array) {
                NMNetwinModel *app = [[NMNetwinModel alloc] init];
                
                [app setAppId:[data objectForKey:@"id"]];
                [app setBundleId:[data objectForKey:@"bundleid"]];
                [app setPoints:[[data objectForKey:@"points"] integerValue]];
                [app setName:[data objectForKey:@"name"]];
                [app setDescription:[data objectForKey:@"description"]];
                [app setIcon:[data objectForKey:@"icon"]];
                [app setNotify:[data objectForKey:@"notify"]];
                
                [ret addObject:app];
            }
            serverResponseBlock(ret, nil);
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        serverResponseBlock(nil, error);
    }];
    
    [operation start];
}

+ (void)startRequestAd:(id)model
{
    if ([model isKindOfClass:[NMNetwinModel class]]) {
        NMNetwinModel *m = (NMNetwinModel *) model;
        NSDictionary *dict = @{@"mid":[UserInfo sharedInstance].USERID,
                               @"mac":[[NMDeviceInfo getMAC] lowercaseString],
                               @"idfa":[[NMDeviceInfo getIDFA] lowercaseString],
                               @"appid":m.appId};
        
        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/Sponsor/%@", [UserInfo sharedInstance].NWR_HOST, m.appId]]];
        [request setHTTPMethod:@"POST"];
        [request setValue:@"application/json; encoding=utf-8" forHTTPHeaderField:@"Content-Type"];
        [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
        NSString *ua = [NSString stringWithFormat:@"%@ %@ (%@; %@; %@)", [NMDeviceInfo getBundleId], [NMDeviceInfo getAppVersion], [NMDeviceInfo getOSVersion], [NMDeviceInfo getMachineDetail], [NMDeviceInfo getLanguage]];
        [request setValue:ua forHTTPHeaderField:@"User-Agent"];
        [request setValue:@"gzip" forHTTPHeaderField:@"Accept-Encoding"];
        
        NSData *requestData =  [NSJSONSerialization dataWithJSONObject:dict options:0 error:nil];
        [request setValue:[NSString stringWithFormat:@"%lu", (unsigned long)requestData.length] forHTTPHeaderField:@"Content-Length"];
        [request setHTTPBody:requestData];
        
        AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
        [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
            NSError *error;
            if (!error) {
                NSString *appStoreURLString = [NSString stringWithFormat:@"itms-apps://itunes.apple.com/app/id%@", m.appId];
                NSURL *appStoreURL = [NSURL URLWithString:appStoreURLString];
                if ([[UIApplication sharedApplication] canOpenURL:appStoreURL]) {
                    [[UIApplication sharedApplication] openURL:appStoreURL];
                }
            }
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        }];
        
        [operation start];
    }
}

@end
